<?php

class Model_Page_User extends RL_Model2 implements ArrayAccess
{
	private $acl_data;
	private $img_storage;
	private $_data = array();

	public function initialize (array $cfg)
	{
		foreach ($cfg as $k=>$v) $this->$k = $v;
	} // end of __construct

	public function getIsAuthenticated ()
	{
		return $this->acl_data['user_id'] ? 1:0;
	} // end of method isAuthenticated
	public function getFullname ()
	{
		return $this->acl_data['user']['fullname'];
	} // end of method readName
	public function getNick ()
	{
		return $this->acl_data['user']['nick'];
	} // end of method readName
	public function getStatus ()
	{
		return $this->acl_data['user']['status'];
	} // end of method readName
	public function getDescription ()
	{
		return $this->acl_data['user']['description'];
	} // end of method readName
	public function getRealName ()
	{
		return $this->acl_data['user']['fullname'];
	} // end of method readName
	public function getOpenidUrl ()
	{
		return $this->acl_data['user']['openid_url'];
	} // end of method openidUrl
	public function getRoleName ()
	{
		return $this->acl_data['role']['name'];
	} // end of method roleName
	public function getRoleTitle ()
	{
		return $this->acl_data['role']['title'];
	} // end of method roleTitle
	public function getMembershipstatus ()
	{
		if (isset($this->_data['membershipstatus'])) return $this->_data['membershipstatus'];

		if (
				 $this->acl_data['role']['name']=="member"
			|| $this->acl_data['role']['name']=="administrator"
			|| $this->acl_data['role']['name']=="moderator"
		) {
			$membershipstatus = 1; // член сообщества
		} else {
			$jq = $this->mm->getModel("JoinQueue","m");
			$jq->setCommunity($this->acl_data['comity_oid']);
			if (($this->acl_data['user_id'] >0) && ($jq->inQueue($this->acl_data['user_id'])) ) {
				$membershipstatus = 2; // заявка в очереди
			} else {
				$membershipstatus = 0; // не член
			}
		}

		return $this->_data['membershipstatus'] = $membershipstatus;
	} // end of method membershipstatus
	public function getOid ()
	{
		return $this->acl_data['user']['user_id'];
	} // end of method oid
	public function getUserId ()
	{
		return $this->acl_data['user']['user_id'];
	} // end of method oid
	public function getAvatar ()
	{
		//if (isset($this->_data['avatar'])) return $this->_data['avatar'];

		if (isset($this->acl_data['user']['avatar'])) {
			$oid = $this->acl_data['user']['avatar'];

			$avatar = $this->getImageStorage()->Get( $oid );
			$avatar['oid'] = $oid;
		} else {
			$avatar = array('path_to_orig' => "images/avatar-100x100.png");
		}

		return $this->_data['avatar'] = $avatar;
	} // end of method oid
	public function getUrl ()
	{
		if (isset($this->_data['url'])) return $this->_data['url'];

		if (isset($this->_data['url'])) {
			$url = $this->_data['url'];
		} else {
			$this->mm->rh->UseClass("RL_URLGenerator");
			$ug = new RL_URLGenerator($this->mm->rh->router->getRule('showfreemember'));
			$ug->setRouter($this->mm->rh->router);
			$ug->setParams(array(
					'module'   => 'Community',
					'action'   => 'ShowMember',
					'path'     => 'members',
					'user_url' => $this->getUserId(),
				));
			$ug->setGet($this->mm->rh->GET);
			$url = $ug->generateUrl();
		}

		return $this->_data['url'] = $url;
	} // end of method oid


	public function offsetGet ($k)
	{
		if (array_key_exists($k, $this->_data)) return $this->_data[$k];
		$mn = 'get'.str_replace('_', '', $k);
		if (method_exists($this,$mn)) return $this->$mn();
		return NULL;
	} // end of method getOffset
	public function offsetSet ($k, $v)
	{
		//if ($k == 'avatar') foreach (debug_backtrace() as $k=>$v) echo "{$v['file']}, {$v['function']}, {$v['line']}\n";
		$this->_data[$k] = $v;
	} // end of method setOffset
	public function offsetExists ($k)
	{
		$mn = 'get'.str_replace($k, '_', '');
		return method_exists($this,$mn) || array_key_exists($k, $this->_data);
	} // end of method offsetExists
	public function offsetUnset ($k)
	{
		unset($this->_data[$k]);
	} // end of method offsetUnset

	public function __toString ()
	{
		return 'Model_Page_User';
	} // end of method __toString

	private function getImageStorage ()
	{
		if (!isset($this->img_storage)) {
			$this->mm->rh->UseClass('stores_ImageStorage');
			$this->img_storage = new ImageStorage( $this->store );
		}
		return $this->img_storage;
	} // end of method getImageObj
}


